package com.horoscope.astrology.zodiac.signs.free.activity

import android.os.Bundle
import android.view.View
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.ImageView
import android.widget.TextView
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.utils.*

class MagicBallActivity : BaseActivity() {

    private lateinit var mTvOk: TextView
    private lateinit var mIvBall: ImageView
    private lateinit var mTip: TextView
    private lateinit var mTvResult: TextView
    private lateinit var fadeIn: Animation

    //    private lateinit var fadeOut: Animation
    private var once = true
    private var timeAD = 0


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_magic_ball)
        // Magic ball页面显示出来
        FlurryEventUtil.putDataToEvent(EventType.TYPE_MAGIC_BALL_SHOW, null, null)
        activityShowAd("Interstitial_Magic")
        initTitleBar(
            this,
            R.id.btn_back_edit_name,
            R.id.text_view_back_title,
            getString(R.string.label_magic_ball)
        )

        initView()
        initListeners()
    }

    private fun initListeners() {
        mTvOk.setOnClickListener {

            if (once) {
                timeAD++
                if (timeAD > 5) {
                    timeAD = 0
                    activityShowAd("Interstitial_Magic")
                }
                // Magic ball 页面点击了 continue
                FlurryEventUtil.putDataToEvent(EventType.TYPE_MAGIC_BALL_CONTINUE, null, null)
                mTvResult.clearAnimation()
                mTvResult.visibility = View.INVISIBLE
                mTip.clearAnimation()
                mTip.visibility = View.VISIBLE

                startAnim()
                once = false
            }
        }

        fadeIn.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationRepeat(animation: Animation?) {
            }

            override fun onAnimationEnd(animation: Animation?) {
                once = true
                mTvOk.text = getString(R.string.label_try_again)
                mTvOk.startAnimation(
                    AnimationUtils.loadAnimation(
                        this@MagicBallActivity,
                        R.anim.fade_in
                    )
                )
            }

            override fun onAnimationStart(animation: Animation?) {
            }

        })
    }

    private fun initView() {
        mTvOk = findViewById(R.id.tv_ok)
        mIvBall = findViewById(R.id.iv_ball)
        mTip = findViewById(R.id.tv_tip)
        mTvResult = findViewById(R.id.tv_result)
        fadeIn = AnimationUtils.loadAnimation(this, R.anim.fade_in)
    }

    private fun startAnim() {
        try {
            mTvOk.startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_out))
            FrameAnimationUtils.startBall(mIvBall)
            var duration = 0
            for (i in 0 until (63)) {
                duration += 50
            }
            mTvOk.postDelayed({
                val anim = AnimationUtils.loadAnimation(this, R.anim.fade_out)
                mTvOk.isEnabled = false
                mTip.startAnimation(anim)
                anim.setAnimationListener(object : Animation.AnimationListener {
                    override fun onAnimationRepeat(animation: Animation?) {
                    }

                    override fun onAnimationEnd(animation: Animation?) {
                        mTvOk.isEnabled = true
                        mTvResult.text = ConstellationUtil.getMagicTip()
                        mTvResult.startAnimation(fadeIn)
                    }

                    override fun onAnimationStart(animation: Animation?) {
                    }

                })
            }, duration.toLong())
        } catch (e: Exception) {
            mTvOk.postDelayed({
                val anim = AnimationUtils.loadAnimation(this, R.anim.fade_out)
                mTip.startAnimation(anim)
                anim.setAnimationListener(object : Animation.AnimationListener {
                    override fun onAnimationRepeat(animation: Animation?) {
                    }

                    override fun onAnimationEnd(animation: Animation?) {
                        mTvResult.text = ConstellationUtil.getMagicTip()
                        mTvResult.startAnimation(fadeIn)
                    }

                    override fun onAnimationStart(animation: Animation?) {
                    }

                })
            }, 3150L)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (AnimationManager.getInstance().getmDisposable() != null) {
            AnimationManager.getInstance().getmDisposable().dispose()
        }
        if (AnimationManager.getInstance().getSubscribe() != null) {
            AnimationManager.getInstance().getSubscribe().dispose()
        }

    }
}